package com.specificgroup.jira.plugin.acpro.action;

import com.atlassian.plugin.webresource.WebResourceManager;
import com.specificgroup.jira.plugin.acpro.exception.AcproException;
import com.specificgroup.jira.plugin.acpro.model.Settings;
import com.specificgroup.jira.plugin.acpro.model.acpro.AcproLoginData;
import com.specificgroup.jira.plugin.acpro.model.acpro.AcproProject;
import com.specificgroup.jira.plugin.acpro.service.AcproManager;
import com.specificgroup.jira.plugin.acpro.session.SessionTempData;
import com.specificgroup.jira.plugin.acpro.session.SessionUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.util.List;

/**
 *
 * @author Ivan Holub
 */
public class SelectAcproProjectAction extends AcproActionSupport {

    private static final String NEXT_ACTION = "AcproSelectItems!default.jspa";

    private static final String RESOURCE_KEY = "com.specificgroup.jira.plugin.acpro.jira-acpro-plugin:acpro-resource-js-select-acpro-project";

    private Logger log = Logger.getLogger(getClass());

    private String acproProjectKey;

    private AcproManager acproManager;

    private List<AcproProject> projects;

    private WebResourceManager webResourceManager;

    public void setAcproProjectKey(String acproProjectKey) {
        this.acproProjectKey = acproProjectKey;
    }

    public SelectAcproProjectAction(AcproManager acproManager, WebResourceManager webResourceManager) {
        this.acproManager = acproManager;
        this.webResourceManager = webResourceManager;
    }

    public List<AcproProject> getProjects() {
        return projects;
    }

    @Override
    public String doDefault() throws Exception {
        if (!hasPermissions()) {
            return PERMISSION_VIOLATION_RESULT;
        }

        webResourceManager.requireResource(RESOURCE_KEY);

        Settings settings = SessionUtil.getSettings();
        AcproLoginData acproLoginData = settings.getAcproLoginData();

        try {
            projects = acproManager.getAcproProjects(acproLoginData);
        } catch (IOException e) {
            log.error("Error", e);
            addErrorMessage(e.toString());
            return ERROR;
        } catch (AcproException e) {
            log.error("Error", e);
            addErrorMessage(e.getMessage());
            return ERROR;
        }

        SessionTempData sessionTempData = SessionUtil.getSessionTempData();
        sessionTempData.setAcproProjects(projects);

        return super.doDefault();
    }

    @Override
    protected String doExecute() throws Exception {
        if (!hasPermissions()) {
            return PERMISSION_VIOLATION_RESULT;
        }
        
        Settings settings = SessionUtil.getSettings();
        settings.setAcproProjectKey(acproProjectKey);
        String redirect = getRedirect(NEXT_ACTION);
        return redirect;
    }

    @Override
    protected void doValidation() {

        boolean isProjectValid = false;
        if (StringUtils.isNotEmpty(acproProjectKey)) {
            isProjectValid = true;
        }

        if (!isProjectValid) {

            webResourceManager.requireResource(RESOURCE_KEY);
            
            SessionTempData sessionTempData = SessionUtil.getSessionTempData();
            projects = sessionTempData.getAcproProjects();

            addErrorMessage(getText("error.project"));
        }

        super.doValidation();
    }

}
